home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Arashi 1.1 Source / Game Source / jaf src / Highscores.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-23  |  11.1 KB  |  583 lines  |  [TEXT/KAHL]

  1. /*
  2. **    Game over, Highscore handling        //Jaf © copyright 1991
  3. */
  4.  
  5. #include "Palettes.h"
  6. #include "VA.h"
  7. #include "STORM.h"
  8. #include "Highscores.h"
  9. #include "NewTitleEffect.h"
  10. #include "PlayOptions.h"
  11.  
  12. #define GAMEOVERDELAY 60
  13.  
  14. extern  int                MainStage;
  15. extern  int                SpecialEvent;
  16. extern  Initials        *HiScores;
  17.  
  18. Highscores(score)
  19. long    score;
  20. {
  21.     int    i,ranking,ranky;
  22.     Handle    ScoreHand;
  23.  
  24.     VASetColors(GetResource('CLOT',1002));
  25.         
  26.     
  27.     if (PlayOptions->restart == 0)
  28.         ScoreHand=GetResource('SCOR',128);
  29.     else 
  30.         ScoreHand=GetResource('SCOR',129);            /* Practice game scores */
  31.     HLock(ScoreHand);
  32.     HiScores=(Initials *)*ScoreHand;
  33.     ranking = Ranking(score);
  34.     VAStep();
  35.     PmBackColor(BGC);
  36.     EraseRect(&VA.frame);
  37.     
  38.     /* GameOver(); */
  39.     titlemain(GAMEOVERPICT, VERTICAL);
  40.     
  41.     EraseRect(&VA.frame);
  42.     
  43.     if(ranking<11)
  44.         WriteCongrats();
  45.     else if(ranking<51)
  46.         WriteCongrats2();
  47.     else {
  48.         WriteNoCongrats(score);
  49.         VAEraseBuffer();
  50.         return;
  51.     }
  52.     
  53.     ranky = ScrollScores(ranking,score);
  54.     GetInitials(ranking,ranky);
  55.     SaveScores(ScoreHand);
  56. }
  57.  
  58. int Ranking(score)
  59. long    score;
  60. {
  61.     int    i,pos,j;
  62.     
  63.     pos=51;    
  64.     /* get ranking */
  65.     for(i=50;i>0;i--) {
  66.         if (score>HiScores[i].score)
  67.             pos = i;
  68.     }
  69.     
  70.     if(pos==51)
  71.         return pos;
  72.         
  73.     /* move lowerthan scores down one step*/
  74.     for(i=50;i>pos;i--) {
  75.         HiScores[i].score = HiScores[i-1].score;
  76.         for(j=0;j<6;j++)
  77.             HiScores[i].name[j] = HiScores[i-1].name[j];
  78.     }
  79.     
  80.     HiScores[pos].score=score;
  81.     for(j=0;j<6;j++)
  82.         HiScores[pos].name[j] = ' ';
  83.     
  84.     return pos;
  85. }
  86.  
  87. #define    OWNRANKINGCOLOR (2*9)
  88.  
  89. int ScrollScores(rank,score)
  90. int        rank;
  91. long    score;
  92. {
  93.     int i,x,y,del,sy,done,doneloop;
  94.     long j;
  95.     int NumScale,TextScale;
  96.     int    returny;
  97.     extern EventRecord    Event;
  98.  
  99.     FlushEvents(everyEvent,0);
  100.     
  101.     TextScale=Getfontscale();
  102.     NumScale=Getfontscale()*2;
  103.  
  104.     del = 20;
  105.     done=0;
  106.     doneloop = -(rank-4);
  107.     if(doneloop>0)
  108.         doneloop=0;
  109.     
  110.     sy=VA.frame.bottom;
  111.     
  112.     do
  113.     {
  114.         VA.segmscale=NumScale;
  115.         sy-=(VA.segmscale*4+4);
  116.         if(sy<VA.frame.bottom/4) {
  117.             doneloop++;
  118.         }
  119.         if(doneloop>0)
  120.             done=1;
  121.         if(sy<VA.frame.bottom/4+(VA.segmscale*4+4) && rank<4)
  122.             done=1;
  123.         PlayA(Bonk,1);
  124.         del--;
  125.         if (del<0)
  126.             del = 0;
  127.  
  128.         Delay(del,&j);
  129.         VACatchUp();
  130.         VAStep();
  131.         GameEvent();
  132.         if(Event.what)
  133.             del = 0;
  134.  
  135.         for(i=1;(i<=rank+2) && (i<51) ;i++) {   /* extra check on high score (mz) */
  136.             y=sy+(i-1)*(VA.segmscale*4+4);
  137.             if( y >= VA.frame.bottom/4 && y < (VA.frame.bottom-(VA.segmscale*4+4))) {                
  138.                 x = VA.frame.right/2;
  139.                 VA.segmscale=TextScale;
  140.                 VA.color=4*9;
  141.                 VADrawNumber(i,VA.frame.right/3,y-1);
  142.                 VAMoveTo(x+6*NumScale,y);
  143.                 if(i==rank) {
  144.                     VA.color=OWNRANKINGCOLOR;
  145.                     VADrawText(".....",0,5);
  146.                     VA.segmscale=NumScale;
  147.                     VADrawNumber(1000000,VA.frame.right/2,y);
  148.                     VADrawNumber(0,VA.frame.right/2-6*(VA.segmscale+7),y);
  149.                     VADrawNumber(score,VA.frame.right/2,y);
  150.                     returny=y;
  151.                 } 
  152.                 else {
  153.                     VA.color=BG2;
  154.                     VADrawText(HiScores[i].name,0,5);
  155.                     VA.segmscale=NumScale;
  156.                     VADrawNumber(1000000,VA.frame.right/2,y);
  157.                     VADrawNumber(0,VA.frame.right/2-6*(VA.segmscale+7),y);
  158.                     VADrawNumber(HiScores[i].score,VA.frame.right/2,y);
  159.                 }
  160.                 if((i==rank+2) || (i==50)) {  /* erase all 50's (mz) */
  161.                     Erase(VA.frame.right/4,y+1,VA.frame.right,y+VA.segmscale*4+5);
  162.                 }    
  163.             }
  164.         }
  165.     } while (!done);
  166.     
  167.     return returny;    
  168. }
  169.  
  170. void WriteCongrats()
  171. {
  172.     int        x,y,i;
  173.     int     len;
  174.     Handle    TextHand;
  175.     char    *str,*Text;
  176.     
  177.     if (PlayOptions->restart == 0)
  178.     {
  179.         TextHand=GetResource('TEXT',1000);
  180.         HLock(TextHand);
  181.         x = VA.frame.right/3.5;
  182.         y = VA.frame.bottom/8;
  183.     }
  184.     else
  185.     {
  186.         TextHand=GetResource('TEXT',1010);
  187.         HLock(TextHand);
  188.         x = VA.frame.right/4;
  189.         y = VA.frame.bottom/8;
  190.     }
  191.     
  192.  
  193.     VA.segmscale = Getfontscale();
  194.  
  195.     Text=(char *)*TextHand;
  196.     len=GetHandleSize(TextHand);
  197.     
  198.     str = Text;
  199.     for(i=0;i<len;i++) {
  200.         if(*(str++) == 13) {
  201.             if(Text<str-1) {
  202.                 VA.color=BG2;
  203.                 VAMoveTo(x,y);
  204.                 VADrawText(Text,0,str-Text-1);
  205.                 VA.color=4*9;
  206.                 VAMoveTo(x+1,y);
  207.                 VADrawText(Text,0,str-Text-1);
  208.             }
  209.             y+=VA.segmscale * 10;
  210.             Text=str;
  211.         }
  212.     }
  213. }
  214. void WriteCongrats2()
  215. {
  216.     int        x,y,i;
  217.     int     len;
  218.     Handle    TextHand;
  219.     char    *str,*Text;
  220.     
  221.     if (PlayOptions->restart == 0)
  222.     {
  223.         TextHand=GetResource('TEXT',132);
  224.         HLock(TextHand);
  225.         x = VA.frame.right/3.5;
  226.         y = VA.frame.bottom/8;
  227.     }
  228.     else
  229.     {
  230.         TextHand=GetResource('TEXT',136);
  231.         HLock(TextHand);
  232.         x = VA.frame.right/4;
  233.         y = VA.frame.bottom/8;
  234.     }
  235.     
  236.  
  237.     VA.segmscale = Getfontscale();
  238.  
  239.     Text=(char *)*TextHand;
  240.     len=GetHandleSize(TextHand);
  241.     
  242.     str = Text;
  243.     for(i=0;i<len;i++) {
  244.         if(*(str++) == 13) {
  245.             if(Text<str-1) {
  246.                 VA.color=BG2;
  247.                 VAMoveTo(x,y);
  248.                 VADrawText(Text,0,str-Text-1);
  249.                 VA.color=4*9;
  250.                 VAMoveTo(x+1,y);
  251.                 VADrawText(Text,0,str-Text-1);
  252.             }
  253.             y+=VA.segmscale * 10;
  254.             Text=str;
  255.         }
  256.     }
  257. }
  258. void WriteNoCongrats(score)
  259. long    score;
  260. {
  261.     int        x,y,i;
  262.     int     len;
  263.     Handle    TextHand;
  264.     char    *str,*Text;
  265.     
  266.     TextHand=GetResource('TEXT',133);
  267.     HLock(TextHand);
  268.     x = VA.frame.right/4;
  269.     y = VA.frame.bottom/4;
  270.  
  271.     VA.segmscale = Getfontscale();
  272.  
  273.     Text=(char *)*TextHand;
  274.     len=GetHandleSize(TextHand);
  275.     
  276.     str = Text;
  277.     for(i=0;i<len;i++) {
  278.         if(*(str++) == 13) {
  279.             if(Text<str-1) {
  280.                 VA.color=BG2;
  281.                 VAMoveTo(x,y);
  282.                 VADrawText(Text,0,str-Text-1);
  283.                 VA.color=4*9;
  284.                 VAMoveTo(x+1,y);
  285.                 VADrawText(Text,0,str-Text-1);
  286.             }
  287.             y+=VA.segmscale * 10;
  288.             Text=str;
  289.         }
  290.     }
  291.     VA.segmscale=Getfontscale();
  292.     x=VA.frame.right/2;
  293.     y=VA.frame.bottom/2;
  294.     VAMoveTo(VA.frame.right/4,y);
  295.     VA.color=2*9;
  296.     VADrawText("score:",0,5);
  297.     VA.segmscale=Getfontscale()*2;
  298.     VADrawNumber(1000000,VA.frame.right/2,y);
  299.     VADrawNumber(0,VA.frame.right/2-6*(VA.segmscale+7),y);
  300.     VADrawNumber(score,VA.frame.right/2,y);
  301.  
  302.     for(i=0;i<GAMEOVERDELAY;i++) {
  303.         if(!(((unsigned int)Random()) % 5))
  304.             Firework();
  305.         GameEvent();
  306.         if((1<<Event.what) & (mUpMask+keyDownMask+keyUpMask))
  307.             i=GAMEOVERDELAY;
  308.         VAStep();
  309.     }        
  310. }
  311.  
  312.  
  313. void GameOver()
  314. {
  315.     int        x,y,i;
  316.     int     len;
  317.     Handle    TextHand;
  318.     char    *str,*Text;
  319.     
  320.     TextHand=GetResource('TEXT',134);
  321.     HLock(TextHand);
  322.     x = VA.frame.right/6;
  323.     y = VA.frame.bottom/2;
  324.  
  325.     VA.segmscale = Getfontscale()*6;
  326.  
  327.     Text=(char *)*TextHand;
  328.     len=GetHandleSize(TextHand);
  329.     
  330.     str = Text;
  331.     for(i=0;i<len;i++) {
  332.         if(*(str++) == 13) {
  333.             if(Text<str-1) {
  334.                 VA.color=BG2;
  335.                 VAMoveTo(x,y);
  336.                 VADrawText(Text,0,str-Text-1);
  337.                 VA.color=BG2;
  338.                 VAMoveTo(x+1,y);
  339.                 VADrawText(Text,0,str-Text-1);
  340.                 VA.color=BG2;
  341.                 VAMoveTo(x+2,y);
  342.                 VADrawText(Text,0,str-Text-1);
  343.             }
  344.             y+=VA.segmscale * 10;
  345.             Text=str;
  346.         }
  347.     }
  348.     for(i=0;i<GAMEOVERDELAY;i++) {
  349.         if(!(((unsigned int)Random()) % 5))
  350.             Firework();
  351.         GameEvent();
  352.         if((1<<Event.what) & (mUpMask+keyDownMask+keyUpMask))
  353.             i=GAMEOVERDELAY;
  354.         VAStep();
  355.     }        
  356. }
  357.  
  358. void    GetInitials(rank,ranky)
  359. int rank,ranky;
  360. {
  361.             long            bigrotho;
  362.             int                rotho=0,oldrotho=1,ang;
  363.             int                x,y,xs,ys,i;
  364.             int                vang,vx,vy,vxs,vys;
  365.             char            bar;
  366.             Point            mouse,oldmouse,center;
  367.             char            name[6],currentchar;
  368.             int                index;
  369.     extern     EventRecord        Event;
  370.             int                Done;
  371.             int                rankx;
  372.             int                TextScale,TextScale2;
  373.             int                delta,history,olddirection;
  374.             char            thechar;
  375.     
  376.     VA.FrameSpeed = 3;
  377.     bigrotho = 0;
  378.     history = 0;    /* No previous mouse rotation.    */
  379.     olddirection = 1;
  380.     
  381.     TextScale=Getfontscale();
  382.     TextScale2=Getfontscale()*2;
  383.  
  384.     rankx= VA.frame.right/2+6*TextScale2;
  385.  
  386.     VA.segmscale=TextScale2;
  387.     VA.offset=1;
  388.     VA.field=1;
  389.     VA.color=2;
  390.  
  391.     vx=VA.segmscale*3+3+12;
  392.     vy=VA.segmscale*4+4+14;
  393.     x=VA.frame.right/2-vx/2-4;
  394.     y=VA.frame.bottom/5+(150*(long)(VA.frame.right))/((Sins[90]/2)+500)-VA.segmscale*4-5;
  395.     
  396.     VAStaticLine(x,y-7,x+vx,y-7);
  397.     VAStaticLine(x,y-6,x,y-7);
  398.     VAStaticLine(x+vx,y-6,x+vx,y-7);    
  399.  
  400.      VAStaticLine(x,y+vy-7,x+vx,y+vy-7);
  401.     VAStaticLine(x,y+vy-7,x,y+vy-8);
  402.     VAStaticLine(x+vx,y+vy-8,x+vx,y+vy-7);
  403.         
  404.     center.h=(VA.DisplayFrame.right+VA.DisplayFrame.left)/2;
  405.     center.v=(VA.DisplayFrame.bottom+VA.DisplayFrame.top)/2;
  406.     
  407.     mouse=center;
  408.     oldmouse=mouse;
  409.  
  410.     Done=0;
  411.     index=0;
  412.     rotho=90;
  413.     currentchar='@';
  414.     
  415.     do {
  416.         vang = rotho;
  417.  
  418.         /* mouse events... */
  419.         GameEvent();
  420.         
  421.         if(Event.what==mouseUp) {
  422.             PlayA(Springy,1);
  423.             HiScores[rank].name[index]=currentchar;
  424.             VAMoveTo(rankx+(TextScale*3+3)*index,ranky);
  425.             VA.segmscale=TextScale;
  426.             VADrawText(" ",0,1);
  427.             VAMoveTo(rankx+(TextScale*3+3)*index,ranky);
  428.             VA.color=OWNRANKINGCOLOR;
  429.             VADrawText(¤tchar,0,1);
  430.             VA.color=BG2;
  431.             VA.segmscale=TextScale2;
  432.             index++;
  433.             if(index==5)
  434.                 Done=1;
  435.         }
  436.         
  437.         if(Event.what==keyDown || Event.what==autoKey) {
  438.             thechar=(Event.message & 0xFF);
  439.             if(thechar>127)
  440.                 thechar-=127;
  441.             /* return */
  442.             if(thechar==13)
  443.                 Done=1;
  444.             /* delete (all controlchars) */
  445.             else if(thechar<32) {
  446.                 if(index>0) {
  447.                     PlayB(Whiz,1);
  448.                     index--;
  449.                     HiScores[rank].name[index]=' ';
  450.                     VAMoveTo(rankx+(TextScale*3+3)*index,ranky);
  451.                     VA.segmscale=TextScale;
  452.                     VADrawText(" ",0,1);
  453.                     VAMoveTo(rankx+(TextScale*3+3)*index,ranky);
  454.                     VA.color=OWNRANKINGCOLOR;
  455.                     VADrawText(".",0,1);
  456.                     VA.color=BG2;
  457.                     VA.segmscale=TextScale2;
  458.                 }
  459.             }
  460.             /* other keys */
  461.             else if(thechar>=32) {
  462.                 PlayA(Bonk,1);
  463.                 HiScores[rank].name[index]=thechar;
  464.                 VAMoveTo(rankx+(TextScale*3+3)*index,ranky);
  465.                 VA.segmscale=TextScale;
  466.                 VADrawText(" ",0,1);
  467.                 VAMoveTo(rankx+(TextScale*3+3)*index,ranky);
  468.                 VA.color=OWNRANKINGCOLOR;
  469.                 VADrawText(&thechar,0,1);
  470.                 VA.color=BG2;
  471.                 VA.segmscale=TextScale2;
  472.                 index++;
  473.                 if(index==5)
  474.                     Done=1;
  475.             }
  476.         }
  477.             
  478.         mouse=center;
  479.         GetSetMouse(&mouse);
  480.         
  481.         delta=mouse.h+history;
  482.         history = delta;
  483.  
  484. #define    MAXROTLETTER    20
  485.         if (delta>MAXROTLETTER)
  486.             delta = MAXROTLETTER;
  487.         if (delta<-MAXROTLETTER)
  488.             delta = -MAXROTLETTER;
  489.         
  490.         history -= delta;
  491.         history = history * 3 / 4;
  492.  
  493. #define    SUBROTATION    (5)
  494.  
  495.         if(delta)
  496.         {    if(delta > 0)    olddirection = SUBROTATION;
  497.             else            olddirection = -SUBROTATION;
  498.             bigrotho += delta;
  499.         }
  500.         else
  501.         {    if(((rotho+ANGLES) % 4) != 1)
  502.             {    bigrotho += olddirection;
  503.             }
  504.         }
  505.  
  506.         bigrotho %= SUBROTATION * ANGLES;
  507.         rotho = bigrotho / SUBROTATION;
  508.         
  509.         if(rotho>=ANGLES) rotho-=ANGLES;
  510.         if(rotho<0) rotho+=ANGLES;
  511.         
  512.         if(rotho != oldrotho)
  513.         {    int        direction;
  514.             int        rotstep;
  515.             
  516.             if(oldrotho < rotho )
  517.             {    rotstep = -4;
  518.                 direction = -1;
  519.             }
  520.             else
  521.             {    rotstep = 4;
  522.                 direction = 1;
  523.             }
  524.             
  525.             oldrotho = rotho;
  526.             ang= rotho;
  527.             for(i=0;i<ANGLES;i+=4) {
  528.  
  529.                 if(ang>=ANGLES) ang-=ANGLES;
  530.                 if(ang<0) ang+=ANGLES;
  531.  
  532.                 if(vang>=ANGLES) vang-=ANGLES;
  533.                 if(vang<0) vang+=ANGLES;
  534.  
  535.                 x=Cosins[ang]/2;
  536.                 y=(Sins[ang]/2)+500;
  537.                 vx=Cosins[vang]/2;
  538.                 vy=(Sins[vang]/2)+500;
  539.     
  540.                 xs=VA.frame.right/2+(x*(long)(VA.frame.right))/y;
  541.                 ys=VA.frame.bottom/5+(150*(long)(VA.frame.right))/y;
  542.                 vxs=VA.frame.right/2+(vx*(long)(VA.frame.right))/vy;
  543.                 vys=VA.frame.bottom/5+(150*(long)(VA.frame.right))/vy;
  544.                 if(ang>87&&ang<92) {
  545.                     VA.color=2*9;
  546.                     currentchar=bar;
  547.                 }
  548.                 else
  549.                     VA.color=BG2;
  550.     
  551.                 if(vy<500)            VA.segmscale=TextScale2+1;
  552.                 else                VA.segmscale=TextScale2;
  553.     
  554.                 VAMoveTo(vxs,vys);
  555.                 VADrawText(" ",0,1);
  556.     
  557.                 if(y<500)             VA.segmscale=TextScale2+1;
  558.                 else                VA.segmscale=TextScale2;
  559.     
  560.                 VAMoveTo(xs,ys);
  561.                 VADrawText(&bar,0,1);
  562.  
  563.                 bar+=direction;
  564.                 if(bar<'@')    bar = ']';
  565.                 if(bar>']') bar = '@';
  566.                 
  567.                 ang+=rotstep;
  568.                 vang+=rotstep;
  569.             }
  570.         }
  571.         VAStep();
  572.     } while(!Done);
  573.     
  574.     VACatchUp();
  575. }
  576.  
  577. void SaveScores(ScoreHandle)
  578. Handle    ScoreHandle;
  579. {
  580.     ChangedResource(ScoreHandle);
  581.     WriteResource(ScoreHandle);
  582.     DetachResource(ScoreHandle);
  583. }